

clear all
cap log close

*Graph style
sysdir set PERSONAL "W:\ado"
set scheme kailaScheme


global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe1 = "year"
global fe2 = "time year" 
global fe3 = "baseid time year" 
global fe_full = "baseid time year age_time" 
global cluster = "shnro"


capture program drop DiD
program define DiD
	args a b c d

	preserve
	
	use "${dataout}allmatches_allyears_matchpast5", clear
	
		
gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys match_id1 year_event: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1 
	
destring suspect_sex, replace 
destring plaintiff_sex, replace 
sort match_id1 year_event time shnro 
bys match_id1 year_event time: ereplace suspect_sex=max(suspect_sex)
bys match_id1 year_event time: ereplace plaintiff_sex=max(plaintiff_sex)

gen mf=(suspect_sex==1 & plaintiff_sex==2 ) 
gen mm=(suspect_sex==1 & plaintiff_sex==1)

keep if `d'==1 

merge m:1 sykstun year_event using  "${dataout}total_firm_topfemale"
drop if _merge ==2
drop _merge
	
rename share_female_top10  share_female_top
	
	* share heterogeneity 
su share_female_top if wp_crime==1 & time==0, detail
local med = r(p50)

g female_firm = (share_female_top>= `med' & share_female_top!=.) & wp_crime==1 & time==0

bys match_id1 year_event: ereplace female_firm=max(female_firm)

gen share_female_top_temp=share_female_top if wp_crime==1 & time==0 
bys match_id1 year_event: ereplace share_female_top_temp=max(share_female_top_temp)



sort shnro year_event time 
gen prior_earnings1=realAllEarn if time==-1 
gen prior_earnings2=realAllEarn if time==-2 
gen prior_earnings3=realAllEarn if time==-3

bysort shnro year_event: ereplace  prior_earnings1=mean( prior_earnings1)
bysort shnro year_event: ereplace  prior_earnings2=mean( prior_earnings2)
bysort shnro year_event: ereplace  prior_earnings3=mean( prior_earnings3)

egen prior_earnings=rmean (prior_earnings1 prior_earnings2 prior_earnings3)

gen share_earning=realAllEarn/prior_earnings

sum share_earning, d 
replace share_earning=10 if share_earning>10

gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

gen age_time=age+time


gen assault =(crimecode6=="210501")
replace assault=1-assault

gen treatPost= treat==1 & time>0
gen iteraction_occ=treatPost*manager_sub
gen iteraction_wagegap=treatPost*manager_sub2
gen iteraction_assault=treatPost*(assault)
gen iteraction_femaletop_dummy=treatPost*female_firm

* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)


	capture erase "${results}DiD_perp_heter_`a'_`c'.tex"
	capture file close fh 

	file open fh using"${results}DiD_perp_heter_`a'_`c'.tex", write replace 
	

		file write fh ///
		"\begin{table}[H]" _n ///
		"\centering" _n ///
		"\begin{threeparttable}"_n ///
		"\caption{Heterogeneity Treatment Effect of Workplace Violence on Perps \label{tab:DiD`a'}}" ///
		"\centering{}" _n ///
		"\begin{tabular}{l l l l l }"_n ///
		"\toprule"_n ///
	     "  & (1) & (2)  & (3)   & (4)   \\"  _n ///
		 "  & `a' &`a' & `a'  & `a'\\"  _n ///
		 "\midrule " _n
		 
		 
		 
   * Main regression will all fixed effects
	reghdfe `a' iteraction_occ treatPost manager_sub, absorb($fe_full)  cluster($cluster)
	local betaMain_occ_`a' = string(_b[iteraction_occ], "%10.3fc")
	local seMain_occ_`a' = string(_se[iteraction_occ], "%10.3fc")
	local betaMain_fe_treat1 = string(_b[treatPost], "%10.3fc")
	local seMain_fe_treat1 = string(_se[treatPost], "%10.3fc")
	local betaMain_fe_manager1 = string(_b[manager_sub], "%10.3fc")
	local seMain_fe_manager1 = string(_se[manager_sub], "%10.3fc")
	
	local obs1 = string((e(N)), "%10.3fc")
	sum `a' if wp_crime == 0 
	local mean_control1 =string( r(mean) , "%10.3fc")
	
	
	
	*Main regression will all fixed effects
	reghdfe `a' iteraction_wagegap treatPost manager_sub2, absorb($fe_full)  cluster($cluster)
	local betaMain_wage_`a' = string(_b[iteraction_wagegap], "%10.3fc")
	local seMain_wage_`a' = string(_se[iteraction_wagegap], "%10.3fc")
	local betaMain_fe_treat2 = string(_b[treatPost], "%10.3fc")
	local seMain_fe_treat2 = string(_se[treatPost], "%10.3fc")
	local betaMain_fe_manager2 = string(_b[manager_sub2], "%10.3fc")
	local seMain_fe_manager2 = string(_se[manager_sub2], "%10.3fc")
	
	local obs2 = string((e(N)), "%10.3fc")
	sum `a' if wp_crime == 0 
	local mean_control2 =string( r(mean) , "%10.3fc")
	
	
		* Main regression will all fixed effects
	reghdfe `a' iteraction_assault treatPost assault, absorb($fe_full)  cluster($cluster)
	local betaMain_perp_`a' = string(_b[iteraction_assault], "%10.3fc")
	local seMain_perp_`a' = string(_se[iteraction_assault], "%10.3fc")
	local betaMain_fe_treat3 = string(_b[treatPost], "%10.3fc")
	local seMain_fe_treat3 = string(_se[treatPost], "%10.3fc")
	local betaMain_fe_manager3 = string(_b[assault], "%10.3fc")
	local seMain_fe_manager3 = string(_se[assault], "%10.3fc")
	
	local obs3 = string((e(N)), "%10.3fc")
	sum `a' if wp_crime == 0 
	local mean_control3 =string( r(mean) , "%10.3fc")

	
	
			* Main regression will all fixed effects
	reghdfe `a' iteraction_femaletop_dummy treatPost, absorb($fe_full)  cluster($cluster)
	local betaMain4 = string(_b[iteraction_femaletop_dummy], "%10.3fc")
	local seMain4 = string(_se[iteraction_femaletop_dummy], "%10.3fc")
	local betaMain_treat4 = string(_b[treatPost], "%10.3fc")
	local seMain_treat4 = string(_se[treatPost], "%10.3fc")
	
	
	local obs4 = string((e(N)), "%10.3fc")
	sum `a' if wp_crime == 0 
	local mean_control4=string( r(mean) , "%10.3fc")
	
	
	


	*file write fh "\textbf{Top 20} \\ " _n
	file write fh "Treatment*PerpManager	 &  `betaMain_occ_`a''  &  &  & &  & \\"   _n
	file write fh "				 & (`seMain_occ_`a'') 	 &   	   &   &    &   &   \\"   _n
	file write fh "Treatment*IncomeGap	 &    & `betaMain_wage_`a'' &  &  &  & \\"   _n
	file write fh "				 & 	 & (`seMain_wage_`a'')   	   &   & &   &   \\"   _n
	
	file write fh "Treatment*Assault &    &  & `betaMain_perp_`a''  &  &  & \\"   _n
	file write fh "				 & 	 &  	   & (`seMain_perp_`a'')    & &   &   \\"   _n
	
	file write fh "Treatment*FemaleManager &    &  &   &  & `betaMain4' & \\"   _n
	file write fh "				 & 	 &  	   &    &   &  (`seMain4')  &   \\"   _n
	
	
	file write fh "Treatment	 &  `betaMain_fe_treat1'  & `betaMain_fe_treat2'  & `betaMain_fe_treat3' & `betaMain_fe_treat4'\\"   _n
	file write fh "				 & (`seMain_fe_treat1') 	 & (`seMain_fe_treat2')  	   & (`seMain_fe_treat3')    & (`seMain_fe_treat4')  \\"   _n
	

	
	
	file write fh "\\"   _n

	

	
	
	file write fh "Year fixed effects   	 &  \checked				  & \checked			&	\checked		&	\checked			\\ " _n 
	file write fh "Time since crime fixed effects 		&  \checked				  & \checked			&	\checked		&	\checked			\\ " _n
	file write fh "Individual fixed effects   	&  \checked				  & \checked			&	\checked		&	\checked				\\ " _n 
	file write fh "Age by time since the event   	&  \checked				  & \checked			&	\checked		&	\checked		\\ " _n 
	
	file write fh "\midrule " _n
	file write fh "N. of Obs. 		  	 		  &  `obs1' 		  & `obs2' 	&  `obs3' & `obs4'    \\ " _n

	file write fh "Non-victims mean  	  		  &  `mean_control1' 		&	`mean_control2'			&  `mean_control3' &  `mean_control4' \\ " _n

	
	file write fh "\bottomrule" _n ///
				  "\end{tabular}" _n ///
				  "\end{threeparttable}" _n ///
				  "\end{table}"
	file close fh 
	restore
	*}
end


DiD "employed" "realAllEarn" "PanelAmf" "mf"
DiD "employed" "realAllEarn" "PanelBmm" "mm"


